#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000+10;

int l[maxn], r[maxn];
void link(int x, int y){
    r[x] = y;
    l[y] = x;
}
int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    int N, M;
    int T = 0;
    while(cin >> N >> M){
        ++T;
        for (int i = 0; i < N; ++i) {
            link(i, i+1);
        }
        link(N, 0);
        bool rev = false;
        for (int i = 0; i < M; ++i) {
            int cmd; cin >> cmd;
            if(cmd == 4){
                rev = !rev;
            }
            else{
                int a, b; cin >> a >> b;
                if(rev && cmd!=3) cmd = 3 - cmd;
                switch(cmd){
                    case 1:{
                        if(l[b] == a) continue;
                        int L = l[b], R = b;
                        link(l[a], r[a]);
                        link(L, a); link(a, R);
                        break;
                    }
                    case 2:{
                        if(r[b] == a) continue;
                        int L = b, R = r[b];
                        link(l[a], r[a]);
                        link(L, a); link(a, R);
                        break;
                    }
                    case 3:{
                        if(r[b] == a) swap(a, b);
                        if(r[a] == b){// a b
                            int L = l[a], R = r[b];
                            link(L, b); link(b, a);link(a, R);
                        }
                        else{ // L1 a R1...L2 b R2
                            int L1 = l[a], R1 = r[a];
                            int L2 = l[b], R2 = r[b];
                            link(L1, b); link(b, R1);
                            link(L2, a); link(a, R2);
                        }
                        break;
                    }
                }
            }
        }
        vector<int> ans; ans.push_back(0);
        for (int i = r[0]; i != 0; i = r[i]) {
            ans.push_back(i);
        }
        long long sum = 0, odd = 0;
        for (int i = 1; i < ans.size(); ++i)  {
            sum += ans[i];
            if(i%2) odd += ans[i];
        }
        cout << "Case " << T << ": ";
        if(!rev) cout << odd << "\n";
        else cout << sum - odd << "\n";
    }
    return 0;
}